//
//  ViewController.m
//  renyikan
//
//  Created by Steven on 2019/7/15.
//  Copyright © 2019 Steven. All rights reserved.
//

#import "ViewController.h"

#import "SingleImageCell.h"
#import "ThreeImageCell.h"
#import "TWWebController.h"

#import "NoImageCell.h"


@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property(nonatomic,weak)IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;
@property(nonatomic, strong)NSMutableArray *datas;

@end

@implementation ViewController
{
    NSInteger _page;
    BOOL _isRefresh;

}


- (NSMutableArray *)datas{
    if (_datas == nil) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getDatas];
    self.tableView.tableFooterView = [UIView new];
    
    _page = 1;

    __weak typeof(self) weakSelf = self;
    [self.tableView pullRefresh:^{
        if (weakSelf.segment.selectedSegmentIndex == 1) {
            self->_isRefresh = NO;
            self->_page ++;
            [weakSelf getWXDatasWithPage:self->_page];
        }else{
            self->_isRefresh = YES;
            self->_page = 1;
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
    
    [self.tableView downRefresh:^{
       
        if (weakSelf.segment.selectedSegmentIndex == 0) {
            [weakSelf getDatas];
        }else{
            self->_isRefresh = YES;
            self->_page = 1;
            [weakSelf getWXDatasWithPage:self->_page];
//            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];

}



- (void)getDatas{
    
    __weak typeof(self) weakSelf = self;
    
    [JFNetworkingManager requestWithURLString:@"http://v.juhe.cn/toutiao/index?type=top&key=fc4bc95ea5444a4c411ec70e7776446c" parameters:nil type: HttpRequestTypePost view: self.view success:^(id responseObject) {
        [weakSelf.tableView.mj_header endRefreshing];
        
        [weakSelf.tableView.mj_footer endRefreshing];
        weakSelf.datas = responseObject[@"result"][@"data"];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)getWXDatasWithPage:(NSInteger)page{
    
    __weak typeof(self) weakSelf = self;
    
    [JFNetworkingManager requestWithURLString: [NSString stringWithFormat:@"http://v.juhe.cn/weixin/query?key=9610538c5345fe8768d547b9bc115420&pno=%d",page] parameters:nil type: HttpRequestTypePost view: self.view success:^(id responseObject) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        
        if (_isRefresh) {
            weakSelf.datas = responseObject[@"result"][@"list"];
        }else{
            [weakSelf.datas addObjectsFromArray:responseObject[@"result"][@"list"]];
            if ([responseObject[@"result"][@"list"] count] < 20) {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                 [weakSelf.tableView.mj_footer endRefreshing];
            }
        }
//        weakSelf.datas = responseObject[@"result"][@"list"];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (IBAction)valueChanged:(UISegmentedControl *)sender {
    
    if (sender.selectedSegmentIndex == 0) {
        [self getDatas];
    }else{
        [self getWXDatasWithPage:_page];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *data = self.datas[indexPath.row];
    if (self.segment.selectedSegmentIndex == 0) {
        if ([data.allKeys containsObject:@"thumbnail_pic_s03"] ||
            [data.allKeys containsObject:@"thumbnail_pic_s02"]) {
            return 180;
        }else{
            return 90;
        }
    }else{
        return 60;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *data = self.datas[indexPath.row];
    
    if (self.segment.selectedSegmentIndex == 0) {
        
        if ([data.allKeys containsObject:@"thumbnail_pic_s03"] ||
            [data.allKeys containsObject:@"thumbnail_pic_s02"]) {
            
            ThreeImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThreeImageCell" forIndexPath:indexPath];
            
            cell.title.text = data[@"title"];
            cell.timeLabel.text = data[@"date"];
            cell.sourceLabel.text = data[@"author_name"];
            
            [cell.firstImage sd_setImageWithURL:[NSURL URLWithString:data[@"thumbnail_pic_s"]]];
            
            [cell.secondImage sd_setImageWithURL:[NSURL URLWithString:data[@"thumbnail_pic_s02"]]];
            
            [cell.thirdImage sd_setImageWithURL:[NSURL URLWithString:data[@"thumbnail_pic_s03"]]];
            
            return cell;
        }else{
            SingleImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SingleImageCell" forIndexPath:indexPath];
            cell.title.text = data[@"title"];
            cell.timeLabel.text = data[@"date"];
            cell.sourceLabel.text = data[@"author_name"];
            [cell.firstImage sd_setImageWithURL:[NSURL URLWithString:data[@"thumbnail_pic_s"]]];
            return cell;
        }
    }else{
        NoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoImageCell" forIndexPath:indexPath];
        cell.titleLabel.text = data[@"title"];
        cell.timeLbel.text = data[@"source"];
        return  cell;
    }
    return nil;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *data = self.datas[indexPath.row];
    TWWebController *vc = [TWWebController new];
    vc.url = [NSURL URLWithString:data[@"url"]];
    vc.hasNavigation = YES;
    vc.title = @"新闻详情";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
